package server;

import common.IEvents;
import java.awt.Robot;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author KEM
 *
 */
public class CommandReceiver extends Thread implements IEvents {

    Socket client = null;
    Robot robot = null;
    boolean loop = true;

    public CommandReceiver(Socket client, Robot robot) {
        this.client = client;
        this.robot = robot;
        start();
    }

    @Override
    public void run() {
        try {
            Scanner scanner = null;
            scanner = new Scanner(client.getInputStream());

            while (loop) {
                Integer command = scanner.nextInt();
                switch (command) {
                    case MOUSE_MOVE:
                        robot.mouseMove(scanner.nextInt(), scanner.nextInt());
                        break;
                    case MOUSE_PRESSED:
                        robot.mousePress(scanner.nextInt());
                        break;
                    case MOUSE_RELEASED:
                        robot.mouseRelease(scanner.nextInt());
                        break;
                    case MOUSE_WHEEL:
                        robot.mouseWheel(scanner.nextInt());
                        break;
                    case KEY_PRESSED:
                        robot.keyPress(scanner.nextInt());
                        break;
                    case KEY_RELEASED:
                        robot.keyRelease(scanner.nextInt());
                        break;
                }
                
            }
        
        } catch (IOException ex) {
            Logger.getLogger(CommandReceiver.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}